package com.epower.web.action.epowerbook;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowerbook.EpowerBookService;
import com.epower.dataobject.EpowerBookDO;
import com.epower.web.action.utils.TemplateUtils;

@Scope("prototype")
@Component()
public class EpowerBookAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	// /*** 查询对象 ***/
	// private EpowerBookDO query;

	// /*** 对象集合 ***/
	private List<EpowerBookDO> epowerBookList;

	/*** BO ***/
	private EpowerBookService epowerBookBO;

	private String bookName;
	/*** 主键 ***/
	private Integer id;

	/*** 查询对象 ***/
	private EpowerBookDO epowerBook;

	// /*** 当前action所在的namespace ***/
	// private String namespace = "epowerbook";
	// /*** 当前action对应的module ***/
	// private String moduleName = "EpowerBook";
	private String pid;
	private Result result = Result.succResult();

	// /***
	// * 列表视图
	// *
	// * @return
	// * @throws Exception
	// */
	// public String listEpowerBook() throws Exception {
	// // 初始化查询条件
	// if (query == null) {
	// query = new EpowerBookDO();
	// }
	// // 查询列表
	// epowerBookList = epowerBookBO.getEpowerBookListPageByOption(query);
	// pages = this.getPagesList(query.getToPage(), query.getTotalPage());
	// return SUCCESS;
	// }
	//
	// public String listEpowerBookByName() {
	// if (bookName == null) {
	// return ERROR;
	// }
	// EpowerBookDO bookDO = new EpowerBookDO();
	// bookDO.setLibname(bookName);
	// epowerBookList = epowerBookBO.getEpowerBookListByName(bookDO);
	// List<TmpBook> tmps = new ArrayList<TmpBook>();
	// for (EpowerBookDO tmp : epowerBookList) {
	// TmpBook tmpBook = new TmpBook();
	// tmpBook.setId(tmp.getId());
	// tmpBook.setPid(tmp.getPid());
	// tmpBook.setLibname(tmp.getLibname());
	// tmps.add(tmpBook);
	// }
	// Map<String, Object> resultMap = new HashMap<String, Object>();
	// resultMap.put("books", tmps);
	// result.setResult(resultMap);
	// result.setResultCode(Result.RESULTCODE_NORMAL);
	// return SUCCESS;
	// }

	/**
	 * http://localhost:8080/epower-site-webapp-1.0.0/jsonepowerbook/
	 * listEpowerBookByPid.action?pid=1
	 * 
	 * @return
	 */
	public String listEpowerBookByPid() throws Exception {
		if (null == pid || "".equals(pid)) {
			// TODO 报错
		}
		String[] pamaters = pid.split("_");
		if (pamaters.length != 3) {
			// TODO 报错
		}
		int id = Integer.valueOf(pamaters[2]);
		int bookGread = Integer.valueOf(pamaters[1]) + 1;
		epowerBookList = epowerBookBO.getEpowerBookListByPid(id);
		List<TmpBook> tmps = new ArrayList<TmpBook>();
		for (EpowerBookDO tmp : epowerBookList) {
			TmpBook tmpBook = new TmpBook();
			tmpBook.setId(tmp.getId());
			tmpBook.setPid(tmp.getPid());
			tmpBook.setLibname(tmp.getLibname());
			tmpBook.setGread(bookGread);
			tmps.add(tmpBook);
		}
		Map<String, Object> model = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (tmps.size() > 0) {
			model.put("books", tmps);
			String tempPath = ServletActionContext.getServletContext().getRealPath("/") + "WEB-INF/freemarker";// 模板路劲
			String bookList = TemplateUtils.html2String(model, "books.ftl", tempPath);// 加载模板
			resultMap.put("bookList", bookList);
		}
		result.setResultCode(Result.RESULTCODE_NORMAL);
		result.setResult(resultMap);
		return SUCCESS;
	}

	// /***
	// * 编辑视图
	// *
	// * @return
	// * @throws Exception
	// */
	// public String viewEditEpowerBook() throws Exception {
	// epowerBook = epowerBookBO.getEpowerBookById(id);
	// return SUCCESS;
	// }
	//
	// /***
	// * 编辑操作
	// *
	// * @return
	// * @throws Exception
	// */
	//
	// public String editEpowerBook() throws Exception {
	// if (!epowerBookBO.updateEpowerBookByOption(epowerBook)) {
	// addActionMessage("编辑失败");
	// return SUCCESS;
	// }
	// addActionMessage("编辑成功");
	// return SUCCESS;
	// }
	//
	// /***
	// * 删除操作
	// *
	// * @return
	// * @throws Exception
	// */
	// public String deleteEpowerBook() throws Exception {
	// if (!epowerBookBO.deleteEpowerBookById(id)) {
	// addActionMessage("删除失败");
	// return SUCCESS;
	// }
	// addActionMessage("删除成功");
	// return SUCCESS;
	// }
	//
	// /***
	// * 添加视图
	// *
	// * @return
	// * @throws Exception
	// */
	// public String viewAddEpowerBook() throws Exception {
	// return SUCCESS;
	// }
	//
	// /***
	// * 添加操作
	// *
	// * @return
	// * @throws Exception
	// */
	// public String addEpowerBook() throws Exception {
	// if (epowerBookBO.addEpowerBook(epowerBook) == null) {
	// addActionMessage("添加失败");
	// return SUCCESS;
	// }
	// addActionMessage("添加成功");
	// return SUCCESS;
	// }

	/*** setter getter start ***/
	// public EpowerBookDO getQuery() {
	// return query;
	// }
	//
	// public void setQuery(EpowerBookDO query) {
	// this.query = query;
	// }

	public List<EpowerBookDO> getEpowerBookList() {
		return epowerBookList;
	}

	public void setEpowerBookList(List<EpowerBookDO> epowerBookList) {
		this.epowerBookList = epowerBookList;
	}

	public EpowerBookService getEpowerBookBO() {
		return epowerBookBO;
	}

	@Resource
	public void setEpowerBookBO(EpowerBookService epowerBookBO) {
		this.epowerBookBO = epowerBookBO;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public EpowerBookDO getEpowerBook() {
		return epowerBook;
	}

	public void setEpowerBook(EpowerBookDO epowerBook) {
		this.epowerBook = epowerBook;
	}

	// public String getNamespace() {
	// return namespace;
	// }
	//
	// public void setNamespace(String namespace) {
	// this.namespace = namespace;
	// }
	//
	// public String getModuleName() {
	// return moduleName;
	// }
	//
	// public void setModuleName(String moduleName) {
	// this.moduleName = moduleName;
	// }

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	/*** setter getter end ***/

	public class TmpBook {

		private Integer id;
		private Integer pid;
		private String libname;
		private Integer gread;

		public Integer getId() {
			return id;
		}

		public void setId(Integer id) {
			this.id = id;
		}

		public String getLibname() {
			return libname;
		}

		public void setLibname(String libname) {
			this.libname = libname;
		}

		public Integer getPid() {
			return pid;
		}

		public void setPid(Integer pid) {
			this.pid = pid;
		}

		public Integer getGread() {
			return gread;
		}

		public void setGread(Integer gread) {
			this.gread = gread;
		}

	}
}
